#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN=100050;
int n,m;
int u[MAXN],v[MAXN],w[MAXN],p[MAXN],r[MAXN];
int cmp(int i,int j){
    return w[i]<w[j];
}
int find(int x){
    return p[x]==x ? x:p[x]=find(p[x]);
}
int Kruskal(){
    int ans=0;
    printf("%d\n",n);
    for(int i=0;i<n;i++){
        p[i]=i;
    }
    for(int i=0;i<m;i++){
        r[i]=i;
    }
    //通过比较w数组取排序r数组
    sort(r,r+m,cmp);
    for(int i=0;i<m;i++){
        int e=r[i];
        int x=find(u[e]);
        int y=find(v[e]);
        if(x!=y){
            ans+=w[e];
            p[x]=y;
        }
    }
    return ans;
}
int main(void){
    char tu,tv;
    int num,c;
    m=0;
    while(~scanf("%d",&n)){
        if(n==0){
            break;
        }
        memset(u,0,sizeof(u));
        memset(v,0,sizeof(v));
        memset(w,0,sizeof(w));
        for(int i=0;i<n-1;i++){
            cin >> tu >> num;
            for(int j=0;j<num;j++){
                cin >> tv >> c;
                u[m]=tu-'A';
                v[m]=tv-'A';
                w[m++]=c;
            }
        }
        printf("%d\n",Kruskal());
    }
    return 0;
}
